Check in beforehand:

  • Can everyone practice giving a thumbs up on Zoom?
  • Is this color scheme okay or should I switch to light mode?

Heads up about the way I code:

  • I like displaying plots in the R Studio console instead of inline (you can switch this setting for each document using the gear)
  • I’ll try to use double quotations (") instead of single (’), but in case I forget…they’re the same!
  • Keyboard shortcuts: https://support.rstudio.com/hc/en-us/articles/200711853-Keyboard-Shortcuts
    • Run code
      • ctrl/cmd + enter
    • %>%
      • ctrl/cmd + shift + m
    • Insert code chunk
      • ctrl/cmd + alt + i
    • Comment out lines
      • ctrl/cmd + shift + c


1: IncrediBILL Palmer Penguins

Same data set as last week: Palmer Penguin dataset from LTER data.

Data exploration

## tibble [344 x 8] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ species          : chr [1:344] "Adelie" "Adelie" "Adelie" "Adelie" ...
##  $ island           : chr [1:344] "Torgersen" "Torgersen" "Torgersen" "Torgersen" ...
##  $ bill_length_mm   : num [1:344] 39.1 39.5 40.3 NA 36.7 39.3 38.9 39.2 34.1 42 ...
##  $ bill_depth_mm    : num [1:344] 18.7 17.4 18 NA 19.3 20.6 17.8 19.6 18.1 20.2 ...
##  $ flipper_length_mm: num [1:344] 181 186 195 NA 193 190 181 195 193 190 ...
##  $ body_mass_g      : num [1:344] 3750 3800 3250 NA 3450 ...
##  $ sex              : chr [1:344] "male" "female" "female" NA ...
##  $ year             : num [1:344] 2007 2007 2007 2007 2007 ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   species = col_character(),
##   ..   island = col_character(),
##   ..   bill_length_mm = col_double(),
##   ..   bill_depth_mm = col_double(),
##   ..   flipper_length_mm = col_double(),
##   ..   body_mass_g = col_double(),
##   ..   sex = col_character(),
##   ..   year = col_double()
##   .. )
## # A tibble: 5 x 8
##   species island bill_length_mm bill_depth_mm flipper_length_~ body_mass_g sex  
##   <chr>   <chr>           <dbl>         <dbl>            <dbl>       <dbl> <chr>
## 1 Chinst~ Dream            55.8          19.8              207        4000 male 
## 2 Chinst~ Dream            43.5          18.1              202        3400 fema~
## 3 Chinst~ Dream            49.6          18.2              193        3775 male 
## 4 Chinst~ Dream            50.8          19                210        4100 male 
## 5 Chinst~ Dream            50.2          18.7              198        3775 fema~
## # ... with 1 more variable: year <dbl>


Static scatterplot

REVIEW: Create a scatterplot using ggplot.

  • x-axis: body_mass_g
  • y-axis: bill_length_mm
  • point color: species

Hint: Use ggplot, geom_point, and aes(color = "")

Tips:

  • ggplot2 is the package, ggplot is the function
  • Things ggplot HAS to know: 1) dataset, 2) aesthetics, 3) geom
  • Dataset and aes can be specified either in ggplot() or any geom_*
  • Aes is for visual changes that depend on a value in the data (as opposed to visual changes that apply to all values)


Beautification

Tips:

# Make the plot pretty

# Save a vector of colors for use in future graphs
col_scale <- c("#A3E7FC", "#32908F", "#26C485") # c() combines values into a vector
names(col_scale) <- c("Adelie", "Gentoo", "Chinstrap") # naming each value to correspond to the species

# New pretty graph
p <- penguins %>% 
  na.omit() %>% # removes ALL lines with NAs from dataset
  ggplot(aes(x = body_mass_g/1000, # can divide by 1000 to make units more manageable
             y = bill_length_mm,
             color = species, # point color depends on species
             shape = sex)) + # point shape depends on sex
  geom_point(size = 1.5, # adjust point size
             stroke = 1.1) + # adjust outline line width
  # scale_color_manual(values = "red", "yellow", "green")) + # DOESN'T WORK
  # scale_color_manual(values = c("lightskyblue", "slateblue4", "darkslategray4")) + 
  scale_color_manual(values = col_scale,
                     labels = c("P. adeliae", "P. antarcticus", "P. papua"), # order matters (unless you use a named vector!)
                     name = "Species") +
  # scale_shape_manual(values = c("\u2600", "\u2601")) + # using unicode values
  # scale_shape_manual(values = c("\u2640", "\u2642")) +
  scale_shape_manual(values = c(19, 21),
                     name = "Sex") +
  theme_minimal() + # one of the default themes
  theme(text = element_text(color = "grey80"), # ALL text color
        title = element_text(face = "bold"), # all TITLES to bold
        plot.title = element_text(color = "lightblue", # PLOT TITLE color
                                  size = 16, # text size
                                  hjust = 0.5, vjust = 5), # adjusting positioning
        axis.text = element_text(color = "grey70"), # AXIS LABELS color
        legend.text = element_text(face = "italic"), # LEGEND LABELS color
        legend.background = element_rect(fill = "grey25", # LEGEND BACKGROUND color
                                         color = NA), # no outline
        plot.margin = margin(25,15,10,10), # remember TRBL (trouble)
        plot.background = element_rect(fill = "grey20"), # entire background color
        panel.grid = element_line(color = "grey60")) + # GRID LINE color
  labs(x = "Body mass (kg)", # Make sure units match!!!
       y = "Bill length (mm)",
       title = "IncrediBILL Palmer Penguins")

p


Saving the plot

  • Better to use code than the “export” button
    • Can control output size and keep values consistent
    • Whatever you do…don’t screenshot! Gives you terrible resolution.
## [1] "C:/Users/kathr/Documents/git-repos/tidytuesday-06oct2020"


Interactive scatterplot

Plotly

  • Plotly is a platform that interacts with R, Python, and Julia
  • Plotly focuses on creating interactive data visualizations and web applications (check out “Dash”)
  • We’ll be using ggplotly, which easily turns ggplots into interactive graphs: https://plotly.com/ggplot2/


2: Flippin’ Palmer Penguins

Interactive scatterplot


Animated scatterplot


Saving the plot

Save as HTML

  • Can save in the HTML output of R markdown
  • Can save as a standalone HTML file using htmltools

Export to plotly account